package org.apogee.mil.client.main.pages.admin.forms.widgets;

import java.util.ArrayList;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.utils.Session;
import org.apogee.mil.shared.Constants.DisplayFormat;
import org.apogee.mil.shared.User;

import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowEditor;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;

public class GroupUser implements IsWidget {
	private User currentUser;
	private ColumnModel cm;
	private Grid<BaseModel> grid;
	private ListStore<BaseModel> userStore = new ListStore<BaseModel>();
	private SimpleComboBox<String> userCombo;
	private Button add;

	public GroupUser() {
		this.currentUser = ServerServiceFactory.getUser(Session.getSessionId());
		userCombo = new SimpleComboBox<String>();
		getUsers();
	}

	private void getUsers() {
		ServerServiceFactory.getAdminService(currentUser.getSessionId()).getUsers(currentUser.getAccessLevel(), new AsyncCallback<ArrayList<BaseModel>>() {
			
			@Override
			public void onSuccess(ArrayList<BaseModel> result) {
				for (BaseModel bm : result) {
					userCombo.add(bm.get(DisplayFormat.USER_FMT).toString());
				}
				userCombo.repaint();
				
			}
			
			@Override
			public void onFailure(Throwable caught) {

			}
		});
		
	}

	@Override
	public Widget asWidget() {
		ContentPanel cp = new ContentPanel();
		cp.getHeader().setVisible(false);
		cp.setSize(275, 400);

		ArrayList<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		
		CellEditor editor = new CellEditor(userCombo){
			@Override
			public Object preProcessValue(Object value) {
				if (value == null) {  
					return value;  
				}  
				return userCombo.findModel(value.toString());

			}

			@Override  
			public Object postProcessValue(Object value) {  
				if (value == null) {  
					return value;  
				}  

				return ((ModelData) value).get(DisplayFormat.DEFAULT);  
			}  
		};
		

		ColumnConfig column = new ColumnConfig();
		column.setId(DisplayFormat.USER_FMT);
		column.setHeader("Users");
		column.setWidth(200);
		column.setRowHeader(true);
		column.setEditor(editor);
		configs.add(column);
		
		CheckColumnConfig delColumn = new CheckColumnConfig(DisplayFormat.DELETE, "Delete", 50);
		CellEditor delColEditor = new CellEditor(new CheckBox());
		delColumn.setEditor(delColEditor);
		configs.add(delColumn);

		cm = new ColumnModel(configs);

		final RowEditor<BaseModel> re = new RowEditor<BaseModel>();

		grid = new Grid<BaseModel>(userStore, cm);
		grid.setBorders(true);
		grid.setColumnLines(true);
		grid.getAriaSupport().setLabelledBy(cp.getHeader().getId()+"-label");
		grid.setSize(275, 335);
		
		grid.addPlugin(re);
		cp.add(grid);

		ToolBar tb = new ToolBar();
		add = new Button("Add User To Group");
		add.setEnabled(false);
		add.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				BaseModel bm = new BaseModel();
				bm.set(DisplayFormat.NEW, DisplayFormat.NEW);
				re.stopEditing(false);
				userStore.insert(bm, userStore.getCount());
				re.startEditing(userStore.indexOf(bm), true);

			}
		});

		tb.add(add);

		cp.setTopComponent(tb);
		
		return cp;
	}
	
	public ListStore<BaseModel> getStore() {
		return userStore;
	}
	
	public void enableAddButton() {
		if (add != null) {
			add.setEnabled(true);
		}
	}

}
